package com.finace.money.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.design.widget.TabLayout;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.ExtractTickBean;
import com.finace.money.data.bean.HistoryLineBean;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.data.bean.chartbean.StickData;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.presenter.KLineViewPresenter;
import com.finace.money.ui.activity.StatusDetalActivity;
import com.finace.money.ui.widget.chartwidget.ChartConstant;
import com.finace.money.ui.widget.chartwidget.ChartView;
import com.finace.money.ui.widget.chartwidget.CrossView;
import com.finace.money.ui.widget.chartwidget.KLineView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/12/1.
 */

public class KLineViewFragment extends BaseFragment implements ChartConstant,ChartView.OnDoubleTapListener {

    @BindView(R.id.tab_time)
    TabLayout tabTime;
    @BindView(R.id.ckf_klineview)
    KLineView kLineView;
    @BindView(R.id.cff_cross)
    CrossView crossView;
    @BindView(R.id.tv_current_price)
    TextView tvCurrentPrice;
    @BindView(R.id.tv_current_chp)
    TextView tvCurrentChp;
    @BindView(R.id.tv_current_high)
    TextView tvCurrentHigh;
    @BindView(R.id.tv_current_yestoday)
    TextView tvCurrentYestoday;
    @BindView(R.id.tv_current_have)
    TextView tvCurrentHave;
    @BindView(R.id.tv_current_low)
    TextView tvCurrentLow;
    @BindView(R.id.tv_current_low_count)
    TextView tvCurrentLowCount;
    @BindView(R.id.tv_current_day_add)
    TextView tvCurrentDayAdd;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    private final int K_DAY = 0;
    private final int K_MINUTE_ONE = 1;
    private final int K_MINUTE_FIVE = 2;
    private final int K_MINUTE_FIFTEEN = 3;
    private final int K_MINUTE_THIRTY = 4;
    private final int K_MINUTE_HOUR = 5;
    private StatusListBean mStatusListBean;
    private String mProductID;
    private int mLevel = 6;
    private ArrayList<HistoryLineBean> lineList;
    private KLineViewPresenter mKLineViewPresenter = new KLineViewPresenter();
    private double mPreSettle = 0;

    @Override
    public int setLayout() {
        return R.layout.fragment_k_line;
    }

    @Override
    public void init() {

        kLineView.setUsedViews(crossView, tvInfo);
        kLineView.setType(1);
        kLineView.setOnDoubleTapListener(this);

        mStatusListBean = ((StatusDetalActivity) getActivity()).getmStatusListBean();
        mProductID = mStatusListBean.getM_Body().getM_ProductID();
        mPreSettle = mStatusListBean.getM_Body().getM_PreSettle().getM_Value();
        initHistory();
        initTab();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_HISTORY);
        intentFilter.addAction(PushReceiver.ACTION_TICK);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(historyReceiver, intentFilter);
        unregisterReceiver(historyReceiver);
        showInfo(mStatusListBean.getM_Body().getTickbean());
    }

    private void initHistory() {

        lineList = (ArrayList<HistoryLineBean>) ACache.get(UIUtil.getContext()).getAsObject(mStatusListBean.getM_Body().getM_ProductID() + mLevel);
        if (null != lineList && lineList.size() > 0) {
            showData();
        } else {
            MessageSendUtil.QueryCandle(mLevel,mProductID);
        }

    }

    BroadcastReceiver historyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String finish = intent.getStringExtra(PushReceiver.FINISH);
            String update = intent.getStringExtra(PushReceiver.UPDATE);
            if (StringUtil.isNotNullString(finish) && finish.equals(PushReceiver.FINISH)) {
                lineList = (ArrayList<HistoryLineBean>) ACache.get(UIUtil.getContext()).getAsObject(mStatusListBean.getM_Body().getM_ProductID()+mLevel);
                if(null != lineList && lineList.size()>0){
                    showData();
                }
            }
            if (StringUtil.isNotNullString(update) && update.equals(PushReceiver.UPDATE)) {
                lineList = (ArrayList<HistoryLineBean>) ACache.get(UIUtil.getContext()).getAsObject(mStatusListBean.getM_Body().getM_ProductID()+mLevel);
                if(null != lineList && lineList.size()>0){
                    showData();
                }
            }
            TickBean tickBean = (TickBean) intent.getSerializableExtra(PushReceiver.DATA);
            if (null != tickBean && mProductID.equals(tickBean.getSymbol())) {
                showInfo(tickBean);
            }

        }
    };

    private void showInfo(TickBean payloadBean) {
        if(null != payloadBean){

            ExtractTickBean extractTickBean = mKLineViewPresenter.extractData(payloadBean, mPreSettle);
            if(null != extractTickBean){
                if (Double.valueOf(payloadBean.getTrade()) > mPreSettle) {
                    tvCurrentPrice.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                    tvCurrentChp.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                } else {
                    tvCurrentPrice.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                    tvCurrentChp.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                }
                tvCurrentPrice.setText(extractTickBean.getCurrentPrice());
                tvCurrentChp.setText(extractTickBean.getCurrentChp());
                tvCurrentHigh.setText(extractTickBean.getCurrentHigh());
                tvCurrentYestoday.setText(extractTickBean.getCurrentYestoday());
                tvCurrentHave.setText(extractTickBean.getCurrentHave());
                tvCurrentLow.setText(extractTickBean.getCurrentLow());
                tvCurrentLowCount.setText(extractTickBean.getCurrentLowCount());
                tvCurrentDayAdd.setText(extractTickBean.getCurrentDayAdd());
            }
        }

    }

    private void showData() {

        ArrayList<StickData> data = mKLineViewPresenter.getData(lineList,mPreSettle);
        kLineView.setDataAndInvalidate(data);
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    private void initTab() {

        tabTime.addTab(tabTime.newTab().setText(R.string.k_day));
        tabTime.addTab(tabTime.newTab().setText(R.string.k_one_minute));
        tabTime.addTab(tabTime.newTab().setText(R.string.k_five_minute));
        tabTime.addTab(tabTime.newTab().setText(R.string.k_fifteen_minute));
        tabTime.addTab(tabTime.newTab().setText(R.string.k_thirty_minute));
        tabTime.addTab(tabTime.newTab().setText(R.string.k_one_hour));
        tabTime.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case K_DAY://日K
                        mLevel = 6;
                        break;
                    case K_MINUTE_ONE://1分
                        mLevel = 1;
                        break;
                    case K_MINUTE_FIVE://5分
                        mLevel = 2;
                        break;
                    case K_MINUTE_FIFTEEN://15分
                        mLevel = 3;
                        break;
                    case K_MINUTE_THIRTY://30分
                        mLevel = 4;
                        break;
                    case K_MINUTE_HOUR://1时
                        mLevel = 5;
                        break;
                }
                initHistory();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @Override
    public void onDoubleTap() {

    }
}
